package com.yisu.auction.api.web.controller.operatorBusiness;

import com.yisu.auction.api.core.service.IMemberService;
import com.yisu.auction.api.core.service.IMobileRechargeService;
import com.yisu.auction.api.core.service.impl.func.MobileChargeUtil;
import com.yisu.auction.api.domain.request.BasePage;
import com.yisu.auction.api.domain.request.operatorBusiness.TelephoneChargeRequest;
import com.yisu.auction.api.domain.request.rechargePackage.RechargeListRequest;
import com.yisu.auction.api.domain.request.rechargePackage.RechargeRequest;
import com.yisu.auction.api.domain.vo.configJsonVos.RechargeTrafficAmountVo;
import com.yisu.auction.api.domain.vo.mobileBelongToPackgeVos.MobileBelongToBackVo;
import com.yisu.auction.api.domain.vo.mobileChargePackageVos.RechargeResponse;
import com.yisu.auction.api.web.controller.BaseController;
import com.yisu.auction.api.web.interceptor.IAuthVerifyAnnotation;
import com.yisu.auction.api.web.utils.MobileBelongToUtil;
import com.yisu.auction.api.web.utils.PingppUtil;
import com.yisu.auction.ewf.common.ali.Request;
import com.yisu.auction.ewf.common.enums.LoginTypeEnum;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.Member;
import com.yisu.auction.ewf.entity.MobileRecharge;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * author: 赵杰
 * Business Description：运营商-话费充值业务
 * createTime：2017/9/1
 */
@Controller
@RequestMapping(value = "/telephoneCharge")
public class TelephoneChargeController extends BaseController {

    @Resource
    private IMobileRechargeService iMobileRechargeService;
    @Resource
    private MobileChargeUtil mobileChargeUtil;
    @Resource
    private IMemberService iMemberService;

    /**
     * author: 赵杰
     * Business Description：获取首页相关业务信息
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/recharge", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult recharge(TelephoneChargeRequest telephoneChargeRequest) throws UnsupportedEncodingException, InterruptedException {
        if (StringUtils.isEmpty(telephoneChargeRequest.getServicenumber()) || StringUtils.isEmpty(telephoneChargeRequest.getId())
                || telephoneChargeRequest.getRechargeType() == 0) {
            return new JsonResult(ResponseCode.FAILURE.code, "参数为空");
        }
        if (telephoneChargeRequest.getRechargeType() == MobileRecharge.RechargeTypeEnum.Traffic.code && StringUtils.isEmpty(telephoneChargeRequest.getPkgtype())) {
            return new JsonResult(ResponseCode.FAILURE.code, "参数为空");
        }

        mobileChargeUtil.setRechargeAmountAndFace(telephoneChargeRequest.getId(), telephoneChargeRequest.getRechargeType(), Integer.valueOf(telephoneChargeRequest.getPkgtype()), telephoneChargeRequest);
        if (telephoneChargeRequest.getAmount() == 0) {
            return new JsonResult(ResponseCode.FAILURE.code, "充值选项不存在！");
        }
        if (!MobileBelongToUtil.checkMobile(telephoneChargeRequest.getServicenumber())) {
            return new JsonResult(ResponseCode.NOT_MOBILE_NUMBER.code, ResponseCode.NOT_MOBILE_NUMBER.msg);
        }
        //获取用户ID
        this.userToken = telephoneChargeRequest.getUserToken();
        telephoneChargeRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        telephoneChargeRequest.setMobileOperator(MobileBelongToUtil.getMobileOperator(telephoneChargeRequest.getServicenumber()));
        //创建充值订单
        String orderNo = iMobileRechargeService.addMobileRecharge(telephoneChargeRequest);

        String chargeStr = PingppUtil.getChargeCommon(orderNo, telephoneChargeRequest.getAmount() + "", telephoneChargeRequest.getPayType(), "", telephoneChargeRequest.getAppSign());
        if (!org.apache.commons.lang.StringUtils.isEmpty(chargeStr)) {
            JsonResult result = new JsonResult();
            result.setData(chargeStr);
            return result;
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }

    /**
     * 获取话费充值列表
     *
     * @return
     */
    @RequestMapping(value = "/getBalanceList", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
    public JsonResult getBalanceList(RechargeListRequest rechargeListRequest) {
        String mobile = "";
        //获取用户ID
        this.userToken = rechargeListRequest.getUserToken();
        if (this.userToken != null && (rechargeListRequest.getMobile() == null || rechargeListRequest.getMobile() == "")) {
            Member member = iMemberService.getMemberByMemberId(this.getToken().getMemberId());
            if (member.getMobile() == null || member.getMobile() == "") {
                mobile = member.getMobile();
            }
        } else {
            mobile = rechargeListRequest.getMobile() == null ? "" : rechargeListRequest.getMobile();
        }
        String supportProvince = mobileChargeUtil.getSupportProvice();
        MobileBelongToBackVo mobileBelongToBackVo = MobileBelongToUtil.getMobileBelongTo(mobile, supportProvince);
        Map map = new HashMap();
        map.put("mobile", mobile);
        map.put("belongToText", mobileBelongToBackVo.getBelongToText());
        map.put("balanceList", mobileChargeUtil.getBalanceList(mobileBelongToBackVo.getUserState()));
        if (mobile == null || mobile == "") {
            return new JsonResult(map);
        }
        if (!MobileBelongToUtil.checkMobile(mobile)) {
            return new JsonResult(ResponseCode.NOT_MOBILE_NUMBER.code, ResponseCode.NOT_MOBILE_NUMBER.msg, map);
        }
        if (mobileBelongToBackVo.getUserState() == 1) {
            return new JsonResult(map);
        } else {
            if (supportProvince == null || supportProvince.isEmpty()) {
                map.put("belongToText", map.get("belongToText") + "功能未开放");
                return new JsonResult(ResponseCode.RECHARGE_AREA_IS_NOT_SUPPORT_AREA.code, "功能未开放", map);
            } else {
                map.put("belongToText", map.get("belongToText") + "仅支持：" + supportProvince + "地区充值");
                return new JsonResult(ResponseCode.RECHARGE_AREA_IS_NOT_SUPPORT_AREA.code, "仅支持：" + supportProvince + "地区充值", map);
            }
        }
    }

    /**
     * 获取流量充值列表
     *
     * @return
     */
    @RequestMapping(value = "/getTrafficList", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
    public JsonResult getTrafficList(RechargeListRequest rechargeListRequest) {
        String mobile = "";
        //获取用户ID
        this.userToken = rechargeListRequest.getUserToken();
        if (this.userToken != null && (rechargeListRequest.getMobile() == null || rechargeListRequest.getMobile() == "")) {
            Member member = iMemberService.getMemberByMemberId(this.getToken().getMemberId());
            if (member.getMobile() == null || member.getMobile() == "") {
                mobile = member.getMobile();
            }
        } else {
            mobile = rechargeListRequest.getMobile() == null ? "" : rechargeListRequest.getMobile();
        }
        String supportProvince = mobileChargeUtil.getSupportProvice();
        MobileBelongToBackVo mobileBelongToBackVo = MobileBelongToUtil.getMobileBelongTo(mobile, supportProvince);
        Map map = new HashMap();
        map.put("mobile", mobile);
        map.put("trafficList", mobileChargeUtil.getTrafficList(mobileBelongToBackVo.getUserState()));
        map.put("belongToText", mobileBelongToBackVo.getBelongToText());
        if (mobile == null || mobile == "") {
            return new JsonResult(map);
        }
        if (!MobileBelongToUtil.checkMobile(mobile)) {
            return new JsonResult(ResponseCode.NOT_MOBILE_NUMBER.code, ResponseCode.NOT_MOBILE_NUMBER.msg, map);
        }
        if (mobileBelongToBackVo.getUserState() == 1) {
            return new JsonResult(map);
        } else {
            if (supportProvince == null || supportProvince.isEmpty()) {
                map.put("belongToText", map.get("belongToText") + "功能未开放");
                return new JsonResult(ResponseCode.RECHARGE_AREA_IS_NOT_SUPPORT_AREA.code, "功能未开放", map);
            } else {
                map.put("belongToText", map.get("belongToText") + "仅支持：" + supportProvince + "地区充值");
                return new JsonResult(ResponseCode.RECHARGE_AREA_IS_NOT_SUPPORT_AREA.code, "仅支持：" + supportProvince + "地区充值", map);
            }
        }
    }

    /**
     * 获取充值列表
     *
     * @return
     */
    @RequestMapping(value = "/getRechargeList", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult getRechargeList(BasePage basePage) {
        //获取用户ID
        this.userToken = basePage.getUserToken();
        basePage.setMemberId(String.valueOf(this.getToken().getMemberId()));
        Map map = new HashMap();
        map.put("rechargeList", iMobileRechargeService.getRechargeList(basePage));
        return new JsonResult(map);
    }

//    /**
//     * Business Description：测试用接口
//     *
//     * @param
//     * @return 系统参数
//     */
//    @RequestMapping(value = "/rechargeTest", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
//    @ResponseBody
//    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
//    public JsonResult rechargeTest(HttpServletRequest request, TelephoneChargeRequest telephoneChargeRequest) throws UnsupportedEncodingException, InterruptedException {
//        System.out.println(request);
//        System.out.println(request.getQueryString());
//        System.out.println(request.getContextPath());
//        if (StringUtils.isEmpty(telephoneChargeRequest.getServicenumber()) || StringUtils.isEmpty(telephoneChargeRequest.getId()) || StringUtils.isEmpty(telephoneChargeRequest.getPkgtype())
//                || telephoneChargeRequest.getRechargeType() == 0) {
//            return new JsonResult(ResponseCode.FAILURE.code, "参数为空");
//        }
//        mobileChargeUtil.setRechargeAmountAndFace(telephoneChargeRequest.getId(), telephoneChargeRequest.getRechargeType(), Integer.valueOf(telephoneChargeRequest.getPkgtype()), telephoneChargeRequest);
//        if (telephoneChargeRequest.getAmount() == 0) {
//            return new JsonResult(ResponseCode.FAILURE.code, "充值选项不存在！");
//        }
//        //创建充值订单
//        String orderNo = iMobileRechargeService.addMobileRecharge(telephoneChargeRequest);
//        mobileChargeUtil.FlowRecharge(orderNo);
//        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
//    }
}